<template>
  <div>
    <el-row class="demo-autocomplete text-center">
      <el-col :span="12">
        <div class="sub-title my-2 text-sm text-gray-600">
          list suggestions when activated
        </div>
        <el-autocomplete
            v-model="state1"
            :fetch-suggestions="querySearch"
            clearable
            class="inline-input w-50"
            placeholder="Please Input"
            @select="handleSelect"
        />
      </el-col>
      <el-col :span="12">
        <div class="sub-title my-2 text-sm text-gray-600">
          list suggestions on input
        </div>
        <el-autocomplete
            v-model="state2"
            :fetch-suggestions="querySearch"
            :trigger-on-focus="false"
            clearable
            class="inline-input w-50"
            placeholder="Please Input"
            @select="handleSelect"
        />
      </el-col>
    </el-row>
    <el-col>
      <el-button @click="defineEs">调用父函数方法</el-button>
    </el-col>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'

interface RestaurantItem {
  value: string
  link: string
}

const state1 = ref('')
const state2 = ref('')

const restaurants = ref<RestaurantItem[]>([])
const querySearch = (queryString: string, cb: any) => {
  const results = queryString
      ? restaurants.value.filter(createFilter(queryString))
      : restaurants.value
  // call callback function to return suggestions
  cb(results)
}
const createFilter = (queryString: string) => {
  return (restaurant: RestaurantItem) => {
    return (
        restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
    )
  }
}
const loadAll = () => {
  return [
    { value: 'vue', link: 'https://github.com/vuejs/vue' },
    { value: 'element', link: 'https://github.com/ElemeFE/element' },
    { value: 'cooking', link: 'https://github.com/ElemeFE/cooking' },
    { value: 'mint-ui', link: 'https://github.com/ElemeFE/mint-ui' },
    { value: 'vuex', link: 'https://github.com/vuejs/vuex' },
    { value: 'vue-router', link: 'https://github.com/vuejs/vue-router' },
    { value: 'babel', link: 'https://github.com/babel/babel' },
  ]
}

const handleSelect = (item: RestaurantItem) => {
  console.log(item)
}
const init = () => {
  let init = {
  } as any
  init.a = 1
  init.b = 2
  init.as = {
    "json":5555
  }as any
  return init
}
console.log(init())
const res = {
  a:1,
  b:2
}

const emit = defineEmits<{
  (e:'dataUp',data:number):void
}>()
const defineEs = () => {
  emit('dataUp',22)
}
defineExpose({
  init,
  res
})

onMounted(() => {
  restaurants.value = loadAll()
})
</script>
